package com.polaris.live.popup.party

import android.content.Context
import android.view.View
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.adapter.PartyTaskAdapter
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.common.ext.convertArabicDigitsToNormal
import com.polaris.live.common.manager.startCountdown
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.util.DateUtils
import com.polaris.live.databinding.PopupPartyTaskBinding
import com.polaris.live.net.userService
import com.polaris.live.resp.back_resp.UserTaskListResp
import com.polaris.live.utils.route.RouterUtils
import kotlinx.coroutines.Job
import kotlin.math.abs

/**
 * PartyTaskPopup
 *
 * @author Created by 半仙 on 2024/2/28/028 15:17
 */
class PartyTaskPopup(
    context: Context,
    private val mUserTaskListResp: UserTaskListResp,
    private val mViewModel: BaseViewModel,
    private val mPageType: Int,
) : BottomPopupView(context) {

    override fun getImplLayoutId(): Int = R.layout.popup_party_task

    private lateinit var mBinding: PopupPartyTaskBinding

    private var mTimeJob: Job? = null

    private val mAdapter by lazy { PartyTaskAdapter() }
    override fun onCreate() {
        super.onCreate()
        mBinding = PopupPartyTaskBinding.bind(popupImplView)
        mBinding.taskRecycler.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(context)
        }
        initData(mUserTaskListResp)
        initListener()
    }

    private fun initListener() {
        mAdapter.setOnItemChildClickListener { _, view, position ->
            val item = mAdapter.getItemOrNull(position) ?: return@setOnItemChildClickListener
            if (item.completed) return@setOnItemChildClickListener
            if (view.id == R.id.go_to_router) {
                if (item.router != null) {
                    RouterUtils.routerGoto(item.router)
                }
                dismiss()
            }
        }
    }

    private fun initData(userTaskListResp: UserTaskListResp) {
        mAdapter.setList(userTaskListResp.tasks)
        if (userTaskListResp.taskEndTime == null) {
            mBinding.tvTime.visibility = View.GONE
        } else {
            val taskTime = userTaskListResp.taskEndTime.toLongOrNull() ?: return

            mTimeJob?.cancel()
            val countTime = taskTime - CacheUtil.getServerTime()
            mTimeJob = mViewModel.viewModelScope.startCountdown(abs(countTime), 1000, {
                val millis2String = DateUtils.formatDuration(it).convertArabicDigitsToNormal()
                mBinding.tvTime.text = millis2String
            }, {
                if (it <= 0) {
                    requestData()
                }
            })
        }
    }

    private fun requestData() {
        mViewModel.request({ userService.getTaskList(mPageType) }, {
            initData(it)
        }, {
            dismiss()
        })
    }

    override fun onDismiss() {
        super.onDismiss()
        mTimeJob?.cancel()
        mTimeJob = null
    }
}